<?php

	class products {
		

		function products() {
			global $admin;

			//if(empty($_SESSION["managerid"])) $admin->login();
		}
		
		function index() {
			global $db, $smarty, $config, $segment, $admin;
			//if(!$admin->rights("products_view")) return;
			//var_dump($smarty->_tpl_vars);
			if($_GET['setYs']==1){
				$params=$_GET;
				$sqlstr = "select hsp.*,hps.pid,hps.pname,hps.spec,hps.pic from {$config->db_prefix}products AS hsp LEFT JOIN  haiou_product_sku AS hps ON hps.id=hsp.product_sku_id where 1=1";
				if (!empty($params['product_id'])) $sqlstr .= " and hps.pid in(".trim($params['product_id']).")";
				if (isset($params['keywords']) && !empty($params['keywords'])){
					$sqlstr .= " and hps.pname like ". $db->qstr("%".$params['keywords']."%");
				}
					
				if (!empty($params['lprice'])) $sqlstr .= " and hsp.pricebuy>=". $db->qstr($params['lprice']);
				if (!empty($params['hprice'])) $sqlstr .= " and hsp.pricebuy<=". $db->qstr($params['hprice']);
					
				if (!empty($params['lstocks'])) $sqlstr .= " and hsp.stocks>=". $db->qstr($params['lstocks']);
				if (!empty($params['hstocks'])) $sqlstr .= " and hsp.stocks<=". $db->qstr($params['hstocks']);
					
				if (!empty($params['lvirstocks'])) $sqlstr .= " and hsp.canbuyout>=". $db->qstr($params['lvirstocks']);
				if (!empty($params['hvirstocks'])) $sqlstr .= " and hsp.canbuyout<=". $db->qstr($params['hvirstocks']);
					
				if (!empty($params['lwaringnum'])) $sqlstr .= " and hsp.shopwarningnum>=". $db->qstr($params['lwaringnum']);
				if (!empty($params['hwaringnum'])) $sqlstr .= " and hsp.shopwarningnum<=". $db->qstr($params['hwaringnum']);
echo $sqlstr;
			}
				
				
			
			
			if(!empty($segment[1])) {
				include_once("mod.classes.php");
				$c = new classes();
				$paths = $c->_getpaths($segment[1]);
				$smarty->assign("paths", $paths);
			}
		}


		function create() {
			//add by gxd
			exit('im sorry，此功能已禁用，请到产品中去添加！');
			
			global $db, $smarty, $config, $segment, $admin;
			//if(!$admin->rights("products_create")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="create") {
				if(empty($_POST["title"])) $errors["empty_title"] = true;
				if(empty($_POST["pricebuy"])) $errors["empty_pricebuy"] = true;
				if(empty($_POST["pricesell"])) $errors["empty_pricesell"] = true;
				if(empty($_POST["priceundersell"])) $errors["empty_priceundersell"] = true;

				if(empty($errors)) {
					$title = $db->qstr($_POST["title"]);
					$spec = $db->qstr($_POST["spec"]);
					$pricebuy = $db->qstr($_POST["pricebuy"]);
					$pricesell = $db->qstr($_POST["pricesell"]);
					$priceundersell = $db->qstr($_POST["priceundersell"]);
					$unit = $db->qstr($_POST["unit"]);
					$intro = $db->qstr($_POST["intro"]);
					$minnum = $db->qstr($_POST["minnum"]);
					$maxnum = $db->qstr($_POST["maxnum"]);
					$shopwarningnum = $db->qstr($_POST["shopwarningnum"]);
					$canbuyout = $db->qstr($_POST["canbuyout"]);
					
					$classid = 0;
					$path = $db->qstr(",0,");
					if(!empty($_POST["classid"])) {
						$classid = $_POST["classid"];
						$path = $db->qstr($db->getone("select path from {$config->db_prefix}classes where id=$classid"));
					}

					$sqlstr = "insert into {$config->db_prefix}products (title, spec, classid, path, pricebuy, pricesell, unit, intro,priceundersell,minnum,maxnum,shopwarningnum,canbuyout) values ($title, $spec, $classid, $path, $pricebuy, $pricesell, $unit, $intro,$priceundersell,$minnum,$maxnum,$shopwarningnum,$canbuyout)";
					$result = $db->execute($sqlstr);
					if($result) header("location:{$config->site_admin}m=inventory&s=products.php");
						
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);								
			}
			$smarty->assign("myclasses",$this->_gettree());
		}
		
		function modify() {
			global $db, $smarty, $config, $segment, $admin;
			//if(!$admin->rights("products_modify")) return;
			
			if(!empty($_POST["action"]) && $_POST["action"]=="modify") {
// 				if(empty($_POST["title"])) $errors["empty_title"] = true;
// 				if(empty($_POST["pricebuy"])) $errors["empty_pricebuy"] = true;
// 				if(empty($_POST["supplier"])) $errors["empty_supplier"] = true;
				if(empty($_POST["unit"])) $errors["empty_unit"] = true;
// 				if(empty($_POST["pricesell"])) $errors["empty_pricesell"] = true;
// 				if(empty($_POST["priceundersell"])) $errors["empty_priceundersell"] = true;
				$barcode = $_POST['barcode'];
				if(!empty($barcode) && strlen($barcode)!=13){
					$errors['barcode_num'] = true;
				}
				$barcode = $barcode?$barcode:'';

				if(empty($errors)) {
					$id = $db->qstr($_POST["id"]);
					$title = $db->qstr($_POST["title"]);
					$spec = $db->qstr($_POST["spec"]);
					$pricebuy = $db->qstr($_POST["pricebuy"]);
					$pricesell = $db->qstr($_POST["pricesell"]);
					$priceundersell = $db->qstr($_POST["priceundersell"]);
					$unit = $db->qstr($_POST["unit"]);
					$intro = $db->qstr($_POST["intro"]);
					$minnum = $db->qstr($_POST["minnum"]);
					$maxnum = $db->qstr($_POST["maxnum"]);
					$shopwarningnum = $db->qstr($_POST["shopwarningnum"]);
					$canbuyout = $db->qstr($_POST["canbuyout"]);
					$wanttobuy = $db->qstr($_POST["wanttobuy"]);
					$supplieid = $_POST["supplier"];
					$supplieid = implode(",",$supplieid);
					$currency_unit = $_POST['currency_unit'];
					
// 					$classid = 0;
// 					$path = $db->qstr(",0,");
// 					$classid_temp = $db->getone("select classid from {$config->db_prefix}suppliers where id=$supplieid");
// 					if($classid_temp) {
// 						$classid = $classid_temp;
// 						$path = $db->qstr($db->getone("select path from {$config->db_prefix}classes where id=$classid"));
// 					}
					$productId = $db->getone("select b.pid from {$config->db_prefix}products as a left join haiou_product_sku as b on a.product_sku_id=b.id where a.id=".$id);
					$skuArr = $db->GetArray('select id from haiou_product_sku where pid='.$productId);
					$skuIds = "";
					foreach ($skuArr as $val)
					{
						$skuIds .= $val['id'].",";
					}
					$skuIds = rtrim($skuIds,",");
					//关联供应商
// 					$sqlstr = "update {$config->db_prefix}products set supplieid='$supplieid' where product_sku_id in ($skuIds)";
// 					$db->execute($sqlstr);
					
					$sqlstr = "update {$config->db_prefix}products set pricesell=$pricesell,priceundersell=$priceundersell, unit=$unit, intro=$intro,minnum=$minnum,maxnum=$maxnum,barcode='$barcode' where id=$id";
					$result = $db->execute($sqlstr);
					
					if(isset($_POST['is_apply_all']) && $_POST['is_apply_all']=="1"){
						$sqlstr = "update {$config->db_prefix}products set shopwarningnum=$shopwarningnum,canbuyout=$canbuyout,wanttobuy=$wanttobuy where product_sku_id in ($skuIds)";
						$result = $db->execute($sqlstr);
					}else{
						$sqlstr = "update {$config->db_prefix}products set shopwarningnum=$shopwarningnum,canbuyout=$canbuyout,wanttobuy=$wanttobuy  where id=$id";
						$result = $db->execute($sqlstr);
					}
					
					/********检查最低真实采购单价min_realpricebuy和avg_realpricebuy字段是否为0，如果为0则更新为pricebuy字段*********/
					$sqlspec = "select min_realpricebuy,avg_realpricebuy from {$config->db_prefix}products where id=$id";
					$min_avg_realpricebuy = $db->getRow($sqlspec);
					$updateStr = "";
					if(!($min_avg_realpricebuy['min_realpricebuy']>0)){
						$updateStr .=" min_realpricebuy=pricebuy,";
					}
					if(!($min_avg_realpricebuy['avg_realpricebuy']>0)){
						$updateStr .=" avg_realpricebuy=pricebuy,";
					}
					if($updateStr!=''){
						$updateStr = trim($updateStr,",");
						$sqlstr = "update {$config->db_prefix}products set {$updateStr} where id=$id";
						$db->execute($sqlstr);
					}
					/*****************/

                    $sqlstr = "update {$config->db_prefix}purchases set product=$title where productid IN($skuIds)";
                    $db->execute($sqlstr);
						
					$sqlstr = "update {$config->db_prefix}stocks set product=$title  where productid IN($skuIds)";
					$db->execute($sqlstr);
						
					$sqlstr = "update {$config->db_prefix}orders set product=$title  where productid IN($skuIds)";
					$db->execute($sqlstr);
					
					if($result) header("location:".(empty($_POST["referer"])?"{$config->site_admin}m=inventory&s=products.php":$_POST["referer"]));
				}
								
				if(!empty($errors)) $smarty->assign("errors", $errors);								
			}
			
			//货币单位
			$sql = "select * from haiou_currency_info order by id";
			$currency_info = $db->GetArray($sql);
			
			$smarty->assign("currency_unit_arr",$currency_info);
			$smarty->assign("myclasses",$this->_gettree());
			$smarty->assign("suppliers",$this->_getsupplierlist());
		}


		function remove() {
			global $db, $segment, $config, $admin;
			//if(!$admin->rights("products_delete")) return;

			$id = $db->qstr($_GET['id']);
			$sqlstr = "delete from {$config->db_prefix}products where id=$id";
			$result = $db->execute($sqlstr);
			if($result) header("location:".(empty($_SERVER['HTTP_REFERER'])?"{$config->site_admin}m=inventory&s=products.php":$_SERVER['HTTP_REFERER']));
		}
		
		private function _gettree($id=0){
		    global $db, $config;
		    $tree =array();
		    $tree_info=$db->GetArray("select * from {$config->db_prefix}classes where parentid=$id");
		    if (empty($tree_info))return null;
		    //var_dump($tree_info);
		    foreach ($tree_info as $value) {
		        $tree[$value['id']]=$value;
		        if($children=$this->_gettree($value['id'])){
		            $tree[$value['id']]['children']=$children;
		        }
		        if ($value['id']==$_GET['id']){
		            $this->parentid=$value['parentid'];
		        }
		    }
		    return $tree;
		}


		private function _getsupplierlist(){
			global $db, $config;
			$list = array();
			$list = $db->GetArray("select * from {$config->db_prefix}suppliers");
			return $list;
		}

		

}
?>